package com.yinhe.music.yhmusic.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.apkfuns.logutils.LogUtils;
import com.hwangjr.rxbus.RxBus;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.socialize.weixin.view.WXCallbackActivity;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.log.MLog;
import com.yinhe.music.yhmusic.model.PayResultInfo;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;
import com.yinhe.music.yhmusic.network.schedulers.BaseSchedulerProvider;
import com.yinhe.music.yhmusic.network.schedulers.SchedulerProvider;
import com.yinhe.music.yhmusic.utils.Preferences;
import com.yinhe.music.yhmusic.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by hasee on 2017/6/19.
 */

public class WXPayEntryActivity extends WXCallbackActivity implements IWXAPIEventHandler {
    private static final String TAG = "WXPayEntryActivity";
    private IWXAPI api;
    private Map<String, String> payMap = new HashMap<>();

    public BaseSchedulerProvider schedulerProvider;
    public CompositeDisposable mDisposable;
    private BaseModel mModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        schedulerProvider = SchedulerProvider.getInstance();
        mDisposable = new CompositeDisposable();
        mModel = new BaseModel();

        api = WXAPIFactory.createWXAPI(this, IConstants.APP_ID, true);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onDestroy() {
        if (mDisposable != null) {
            mDisposable.dispose();
        }
        super.onDestroy();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            MLog.debug(TAG, "onPayFinish,errCode=" + baseResp.errCode);
            if (baseResp.errCode == 0) {
                LogUtils.d(baseResp);
                Disposable disposable = mModel.getPayResult(Preferences.getOrderNo())
                        .compose(ResponseTransformer.handleResult())
                        .compose(schedulerProvider.applySchedulers())
                        .subscribe(payResultInfo -> {
                            parsePayResult(payResultInfo);
                        }, throwable -> {
                            ToastUtils.show(R.string.pay_fail_tip);
                        });
                mDisposable.add(disposable);
            } else {
                ToastUtils.show(R.string.pay_fail_tip);
            }
            finish();
        }
    }

    private void parsePayResult(PayResultInfo payResultInfo) {
        int payStatus;
        if (payResultInfo.isPaySuccess()) {
            payStatus = 1;
            ToastUtils.show(R.string.pay_success_tip);
        } else {
            payStatus = 0;
            ToastUtils.show(R.string.pay_fail_tip);
        }
        RxBus.get().post(RxBusEventType.Pay.PAY_STATUS, (Integer) payStatus);
    }
}
